/*
 * @Author: Song Xinxin
 * @Date: 2022-10-31 17:12:51
 * @LastEditors: Song Xinxin
 * @LastEditTime: 2022-11-08 11:58:56
 * @FilePath: \WYW_0419_Oasis\JavaScripts\Modules\NPC\Module_NPC.ts
 * @Description: NPC模块
 * 
 * Copyright (c) 2022 by error: git config user.name && git config user.email & please set dead value or install git, All Rights Reserved. 
 */
import { DataCenterS, ModuleC, ModuleManager, ModuleS } from "odin";
import { Consts } from "../../Consts";
import { ModuleC_Dress, ModuleS_Dress } from "../Dress/Module_Dress";
import { Data_PlayerAttr } from "../PlayerAttr/Data_PlayerAttr";
import { ModuleS_Skill } from "../Skill/Module_Skill";
import { ModuleS_Sound } from "../Sound/Module_Sound";

//技能类型
export namespace Skill
{
	export enum type
    {
        Fly = 0,
        Hide = 1
    }
}

//NPC模块服务端
export class ModuleS_NPC extends ModuleS<ModuleC_NPC,null>
{

    //NPC1
    NPC_1:GamePlay.Humanoid;
    HP_1:number;
    SkillType_1:number;

    //NPC2
    NPC_2:GamePlay.Humanoid;
    HP_2:number;
    SkillType_2:number;

    public async onAwake(): Promise<void> 
    {
        //生成NPC并初始化
        (this.NPC_1 = await MWCore.GameObject.asyncSpawnGameObject(Consts.NPC_GUID) as GamePlay.Humanoid).ready().then(()=>
        {
            this.rebirth(this.NPC_1);
        });
        (this.NPC_2 = await MWCore.GameObject.asyncSpawnGameObject(Consts.NPC_GUID) as GamePlay.Humanoid).ready().then(()=>
        {
            this.rebirth(this.NPC_2)
        })
        
    }

    /**
     * 击中NPC
     * @param player 开枪的玩家
     * @param NPC 被击中的NPC
     */
    public hitNPC(player:GamePlay.Player,NPC:GamePlay.Humanoid):void
    {
        let HP:number;
            let SkillType:number;
            if(NPC == this.NPC_1)
            {
                HP = this.HP_1;
                SkillType = this.SkillType_1;
            }
            else
            {
                HP = this.HP_2;
                SkillType = this.SkillType_2;
            }
            if(HP > 0 )
            {
                let kill = this.loseHP(NPC);
                if(kill == true)
                {
                    this.reward(player,SkillType);
                }
            }
    }

    /**
     * 初始化NPC
     */
    public initNPC():void
    {
        ModuleManager.instance.getModule(ModuleS_Dress).sendNPC_Dress(this.NPC_1,Consts.Dress.Flamer.all);
        ModuleManager.instance.getModule(ModuleS_Dress).sendNPC_Dress(this.NPC_2,Consts.Dress.Flamer.all);
        // this.rebirth(this.NPC_1);
        // this.rebirth(this.NPC_2);
    }

    /**
     * NPC掉血
     * @param NPC 
     * @returns 是否被击杀
     */
    public loseHP(NPC:GamePlay.Humanoid):boolean
    {
        ModuleManager.instance.getModule(ModuleS_Sound).hitPlayerSound(NPC);
        let kill:boolean = false;
        if(NPC == this.NPC_1)
        {
            //判断NPC受到攻击后是否死亡
		    if(this.HP_1 > Consts.WeaponAttr.damage)
		    {
			    //NPC血量减少
			    this.HP_1 = this.HP_1 - Consts.WeaponAttr.damage;
	    	}
		    else
		    {      
			    //NPC死亡
			    this.death(NPC);
			    kill = true;
		    }
        }
        else
        {
            //判断NPC受到攻击后是否死亡
		    if(this.HP_2 > Consts.WeaponAttr.damage)
		    {
			    //NPC血量减少
			    this.HP_2 = this.HP_2 - Consts.WeaponAttr.damage;
	    	}
		    else
		    {      
			    //NPC死亡
			    this.death(NPC);
			    kill = true;
		    }
        }

		return kill;
    }

    /**
     * NPC死亡
     * @param NPC 死亡的NPC
     */
    public death(NPC:GamePlay.Humanoid):void
    {
        if(NPC == this.NPC_1)
        {
            this.HP_1 = 0;
        }
        else
        {
            this.HP_2 = 0;
        }
        NPC.ragdoll(true);
        setTimeout(()=>
        {
            //NPC复活
            this.rebirth(NPC);
        },Consts.NPC_RES_TIME*1000)

    }

    /**
     * 击杀NPC的奖励
     * @param player 击杀NPC的玩家
     * @param SkillType NPC携带的技能
     */
    public reward(player:GamePlay.Player,SkillType:number):void
    {
         //解锁对应技能
         let skilltype = SkillType;
         let skill = ModuleManager.instance.getModule(ModuleS_Skill);
         switch(skilltype)
         {
             case Skill.type.Fly:
                {
                    skill.UnlockFly(player);
                    break;
                }
             case Skill.type.Hide:
                {
                    skill.UnlockHide(player);
                    break;
                }
             default:break;
         }
         //收集物数量加1
        let data = DataCenterS.instance.getModuleData(player,Data_PlayerAttr);
        data.addcount(1);
        data.saveData(true);
    }

    /**
     * NPC复活
     * @param NPC 复活的NPC
     */
    public rebirth(NPC:GamePlay.Humanoid):void
    {
        //随机生成0~3中的数
        let random = Math.round(Math.random()*3)
        //将NPC放到对应寻路目标点
        NPC.location = Consts.TARGET_POSITION[random];
        setTimeout(()=>
        {
            if(NPC == this.NPC_1)
            {
                this.HP_1 = 30;
                this.SkillType_1 = Math.round(Math.random());
            }
            else
            {
                this.HP_2 = 30;
                this.SkillType_2 = Math.round(Math.random());
            }
            NPC.ragdoll(false);
            this.findPath(NPC);
        },Consts.NPC_RES_TIME*1000);
        
    }

    /**
     * 向4个目标点中的随机1个不断寻路
     * @param NPC 
     */
    public findPath(NPC:GamePlay.Humanoid):void
    {
        let random = Math.round(Math.random()*3)
        let target = Consts.TARGET_POSITION[random];
        GamePlay.moveTo(NPC,target,0,()=>
        {
            this.findPath(NPC);
        })
    }


}

//NPC模块客户端
export class ModuleC_NPC extends ModuleC<ModuleS_NPC,null>
{

}